<?php

namespace app\controllers;

use PHPExcel;
use PHPExcel_Exception;
use PHPExcel_IOFactory;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use app\modules\clinical\models\Patient;

class ExportController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /**
     * @return string
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     * @throws \PHPExcel_Writer_Exception
     * @author sally
     * @date   2019-05-19
     */
    public function actionPatient()
    {
        set_time_limit(3600);
        $objPHPExcel = new PHPExcel();

        // config PHPExcel
        $objPHPExcel
              ->getProperties()
              ->setCreator(Yii::$app->user->identity->username)
              ->setTitle("患者基本信息导出")
              ->setSubject("患者基本信息");

        $data = Patient::find()->where(['delete_at' => 0])->all();

        try {
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', "患者id")
                ->setCellValue('B1', "病案号")
                ->setCellValue('C1', "分子号")
                ->setCellValue('D1', "患者姓名")
                ->setCellValue('E1', "出生日期")
                ->setCellValue('F1', "性别")
                ->setCellValue('G1', "民族")
                ->setCellValue('H1', "婚姻状况")
                ->setCellValue('I1', "籍贯")
                ->setCellValue('J1', "详细通讯地址")
                ->setCellValue('K1', "文化程度")
                ->setCellValue('L1', "职业")
                ->setCellValue('M1', "医保")
                ->setCellValue('N1', "患者关系1")
                ->setCellValue('O1', "联系电话1")
                ->setCellValue('P1', "电子邮箱1")
                ->setCellValue('Q1', "患者关系2")
                ->setCellValue('R1', "联系电话2")
                ->setCellValue('S1', "电子邮箱2")
                ->setCellValue('T1', "有无家族史")
                ->setCellValue('U1', "父母是否近亲")
                ->setCellValue('V1', "既往史")
                ->setCellValue('W1', "诊断")
                ->setCellValue('X1', "发病年龄")
                ->setCellValue('Y1', "介绍人")
                ->setCellValue('Z1', "就诊医院")
                ->setCellValue('AA1', "配合程度")
                ->setCellValue('AB1', "就诊医院")
                ->setCellValue('AC1', "死亡时间")
                ->setCellValue('AD1', "备注")
                ->setCellValue('AE1', "惯用手")
                ->setCellValue('AF1', "创建人")
                ->setCellValue('AG1', "创建日期")
                ->setCellValue('AH1', "修改人")
                ->setCellValue('AI1', "修改日期");

            $i = 2;
            foreach ($data as $key => $value) {
                $objPHPExcel->getActiveSheet()
                   ->setCellValue('A'.$i, $value['pk_id'])
                   ->setCellValue('B'.$i, $value['pt_id'])
                   ->setCellValue('C'.$i, $value['dna_id'])
                   ->setCellValue('D'.$i, $value['pt_name'])
                   ->setCellValue('E'.$i, $value['pt_birth'])
                   ->setCellValue('F'.$i, Patient::getParams('sex', $value['pt_sex']))
                   ->setCellValue('G'.$i, Patient::getParams('nation', $value['pt_nation']))
                   ->setCellValue('H'.$i, Patient::getParams('edu', $value['pt_marry']))
                   ->setCellValue('I'.$i, $value['pt_native'])
                   ->setCellValue('J'.$i, $value['pt_addr'])
                   ->setCellValue('K'.$i, Patient::getParams('marry', $value['pt_edu']))
                   ->setCellValue('L'.$i, $value['pt_vocation'])
                   ->setCellValue('M'.$i, Patient::getParams('insurance', $value['pt_insu']))
                   ->setCellValue('N'.$i, Patient::getParams('relation', $value['rel_relation1']))
                   ->setCellValue('O'.$i, $value['rel_phone1'])
                   ->setCellValue('P'.$i, $value['rel_email1'])
                   ->setCellValue('Q'.$i, Patient::getParams('relation', $value['rel_relation2']))
                   ->setCellValue('R'.$i, $value['rel_phone2'])
                   ->setCellValue('S'.$i, $value['rel_email2'])
                   ->setCellValue('T'.$i, Patient::getParams('youwu', $value['pt_fml']))
                   ->setCellValue('U'.$i, Patient::getParams('yesno', $value['pt_jinqin']))
                   ->setCellValue('V'.$i, Patient::getHistory($value['pt_history']))
                   ->setCellValue('W'.$i, is_numeric($value['pt_disease']) ? Patient::getParams('disease', $value['pt_disease']) : $value['pt_disease'])
                   ->setCellValue('X'.$i, $value['pt_age'] == 0 ? '' : $value['pt_age'])
                   ->setCellValue('Y'.$i, $value['pt_intro'])
                   ->setCellValue('Z'.$i, $value['pt_label'])
                   ->setCellValue('AA'.$i, Patient::getParams('yesno', $value['isCooperate']))
                   ->setCellValue('AB'.$i, $value['pt_hsp'])
                   ->setCellValue('AC'.$i, ($value['pt_dead'] == '1001-01-01') ? '否' : '是 ('.$value['pt_dead'].')')
                   ->setCellValue('AD'.$i, $value['pt_note'])
                   ->setCellValue('AE'.$i, Patient::getParams('customary', $value['pt_customary']))
                   ->setCellValue('AF'.$i, $value['crt_name'])
                   ->setCellValue('AG'.$i, $value['crt_date'])
                   ->setCellValue('AH'.$i, $value['update_name'])
                   ->setCellValue('AI'.$i, $value['update_at']);
                $i ++;
            }
        } catch (PHPExcel_Exception $e) {
            throw $e;
        }

        // get the spreadsheet
        $excel_name = '患者基本信息导出'.date("Ymd");
        $objActSheet =$objPHPExcel->getActiveSheet();
        $objActSheet->setTitle($excel_name);

        header('Content-Type:application/vnd.ms-excel');
        header('Content-Disposition:attachment;filename="'.$excel_name.'.xls"');
        header('Cache-Control:max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');

        return ;
    }
}
